﻿// EditorBase.cs
//
// Copyright (c) 2007 Mike Strobel
//
// This source code is subject to the terms of the Microsoft Reciprocal License (Ms-RL).
// For details, see <http://www.opensource.org/licenses/ms-rl.html>.
//
// All other rights reserved.

using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Supremacy.Editor
{
    public class EditorBase : UserControl, INotifyPropertyChanged
    {
        public static readonly DependencyProperty HasChangesProperty;
        protected static readonly DependencyPropertyKey HasChangesPropertyKey;

        static EditorBase()
        {
            HasChangesPropertyKey = DependencyProperty.RegisterReadOnly(
                "HasChanges",
                typeof(bool),
                typeof(EditorBase),
                new FrameworkPropertyMetadata(false));

            HasChangesProperty = HasChangesPropertyKey.DependencyProperty;
        }

        public EditorBase()
        {
            this.Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                EditorApp.Current.PropertyChanged += OnEditorAppPropertyChanged;
            }
        }

        private void OnEditorAppPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "EditorContext")
                OnPropertyChanged("EditorContext");
        }

        public bool HasChanges
        {
            get { return (bool)this.GetValue(HasChangesProperty); }
            protected set { SetValue(HasChangesPropertyKey, value); }
        }

        public EditorContext EditorContext
        {
            get { return EditorApp.Current.EditorContext; }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    }
}
